//
//  WeddingCountDownView.m
//  BaseProject
//
//  Created by 王洋 on 2020/7/7.
//  Copyright © 2020 王洋. All rights reserved.
//
#import <BRPickerView/BRDatePickerView.h>

#import "WeddingCountDownView.h"

@interface WeddingCountDownView ()

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topDistance;
@property (weak, nonatomic) IBOutlet UILabel *titleLab;


@property (weak, nonatomic) IBOutlet UILabel *leaveDayLab;

@property (weak, nonatomic) IBOutlet UILabel *dateLab;

@property (weak, nonatomic) IBOutlet UILabel *nongliLab;

@property (weak, nonatomic) IBOutlet UIButton *handleBtn;

@property (weak, nonatomic) IBOutlet UIButton *sureBtn;

@property (weak, nonatomic) IBOutlet UIButton *cancleBtn;


@property (weak, nonatomic) IBOutlet UIView *bgView;

@property (weak, nonatomic) IBOutlet UIView *pickerBGView;

@property (nonatomic, strong) BRDatePickerView *datePickerView;

@property (nonatomic, assign) WeddingCountViewType viewType;

@property (nonatomic, copy) NSString *dateValue;

@end

@implementation WeddingCountDownView

- (void)awakeFromNib {
    [super awakeFromNib];
        
    [self.pickerBGView addSubview:self.datePickerView];
    [self.datePickerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(0);
    }];
        
    [self.datePickerView addPickerToView:self.pickerBGView];
    
    WEAKSELF
    [self addGestureTapEventHandle:^(id  _Nonnull sender, UITapGestureRecognizer * _Nonnull gestureRecognizer) {
        if (weakSelf.WeddingCountDownBlock) {
            weakSelf.WeddingCountDownBlock(WeddingCountActionType_close,@"");
        }
    }];
    
    self.datePickerView.changeBlock = ^(NSDate * _Nullable selectDate, NSString * _Nullable selectValue) {

        NSDateFormatter * formatter=[[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"yyyy-MM-dd"];
        
        weakSelf.dateValue = [Utilities getTimeFromTimestamp:[Utilities getTimestampWithDate:selectDate] AndFormater:formatter];
        
        NSLog(@"%@",weakSelf.dateValue);
    };
}

- (void)fillViewWithModel:(WeddingCountDownModel *)model type:(WeddingCountViewType)type {

    [self refreshViewWithType:type];
    
    if (model.betweenDate >= 0) {
        self.dateLab.text = model.gregorianCalendar;
        self.nongliLab.text = model.lunar;
        NSString *leaveDay = [NSString stringWithFormat:@"%ld天",(long)model.betweenDate];
        
        //富文本
        NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithString:leaveDay];
        NSRange countRange = NSMakeRange(0, leaveDay.length-1);
        NSRange datRange = NSMakeRange(leaveDay.length-1,1);
        // 设置字体大小
        [attrStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:66 weight:UIFontWeightSemibold] range:countRange];
        [attrStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:18] range:datRange];
        self.leaveDayLab.attributedText = attrStr;
    } else {
        
        self.dateLab.text = model.gregorianCalendar;
        self.nongliLab.text = model.lunar;
        NSString *leaveDay = @"婚期已过";
        
        //富文本
        NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithString:leaveDay];
        NSRange countRange = NSMakeRange(0, leaveDay.length);
        // 设置字体大小
        [attrStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:33 weight:UIFontWeightSemibold] range:countRange];
        self.leaveDayLab.attributedText = attrStr;

    }

}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    WEAKSELF
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [weakSelf.handleBtn pp_clipCorners:UIRectCornerAllCorners radius:weakSelf.handleBtn.frame.size.height/2];
        [weakSelf.bgView pp_clipCorners:UIRectCornerAllCorners radius:10];
        
        [weakSelf.sureBtn pp_clipCorners:UIRectCornerAllCorners radius:weakSelf.handleBtn.frame.size.height/2];
        [weakSelf.cancleBtn pp_clipCorners:UIRectCornerAllCorners radius:weakSelf.handleBtn.frame.size.height/2];

    });

}

- (void)refreshViewWithType:(WeddingCountViewType)type {
    
    self.viewType = type;
    switch (type) {
        case WeddingCountViewType_None:{
            
            self.titleLab.text = @"设置你的婚期";
            self.titleLab.font = [UIFont systemFontOfSize:22 weight:UIFontWeightMedium];
            
            self.topDistance.constant = 58;
            
            self.handleBtn.hidden = YES;
            self.cancleBtn.hidden = NO;
            self.sureBtn.hidden = NO;
            
            self.leaveDayLab.hidden = YES;
            self.pickerBGView.hidden = NO;
            
        } break;
        case WeddingCountViewType_Sel:{
            
            self.handleBtn.hidden = NO;
            self.cancleBtn.hidden = YES;
            self.sureBtn.hidden = YES;
            
            self.pickerBGView.hidden = YES;

        } break;
        case WeddingCountViewType_Edit:{
            
            self.handleBtn.hidden = YES;
            self.cancleBtn.hidden = NO;
            self.sureBtn.hidden = NO;
            
            self.pickerBGView.hidden = NO;
            
        } break;
        default:
            break;
    }
}

- (IBAction)closeAction:(id)sender {
    if (self.WeddingCountDownBlock) {
        self.WeddingCountDownBlock(WeddingCountActionType_close,@"");
    }
}

- (IBAction)editAction:(id)sender {
    
    [self refreshViewWithType:WeddingCountViewType_Edit];
}

- (IBAction)sureAction:(id)sender {
    
    if (self.WeddingCountDownBlock) {
        self.WeddingCountDownBlock(WeddingCountActionType_update,self.dateValue);
    }
}

- (IBAction)cancleAction:(id)sender {
    
    switch (self.viewType) {
        case WeddingCountViewType_None:{
            
            if (self.WeddingCountDownBlock) {
                self.WeddingCountDownBlock(WeddingCountActionType_close,@"");
            }
            
        } break;
        case WeddingCountViewType_Edit:{
            [self refreshViewWithType:WeddingCountViewType_Sel];
        } break;
        default:
            break;
    }
}

- (IBAction)handleAction:(id)sender {
    if (self.WeddingCountDownBlock) {
        self.WeddingCountDownBlock(WeddingCountActionType_close,@"");
    }
}

- (BRDatePickerView *)datePickerView {
    
    if (!_datePickerView) {
        _datePickerView = [[BRDatePickerView alloc]init];
        _datePickerView.pickerMode = BRDatePickerModeYMD;
        _datePickerView.isAutoSelect = NO;
        _datePickerView.showToday = YES;
        
        if ([TTUserModel sharedObject].marryTime.length > 0) {
            _datePickerView.selectDate = [NSDate dateWithString:[TTUserModel sharedObject].marryTime format:@"yyyy-MM-dd"];
            
           NSDate *minDate = [Utilities compareMinDate:[NSDate dateWithString:[TTUserModel sharedObject].marryTime format:@"yyyy-MM-dd"] withDateStr:[Utilities getDateWithTimestamp:[Utilities getCurrentTimestamp]]];
            
            _datePickerView.minDate = minDate;
        } else {
            _datePickerView.minDate = [Utilities getDateWithTimestamp:[Utilities getCurrentTimestamp]];

        }
    }
    return _datePickerView;
}



@end
